package com.ymatou.operate.trading.api.testcase;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.enums.LogisticsTypeEnum;
import com.ymatou.operate.trading.api.parameter.SyncDomesticDeliveredInfoBean;
import com.ymatou.operate.trading.api.service.SyncDomesticDeliveredInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;

/**
 * 同步国内发货信息接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SyncDomesticDeliveredInfo {
    private static SyncDomesticDeliveredInfoBean syncdomesticdeliveredinfoBean;
    private static SyncDomesticDeliveredInfoCall syncdomesticdeliveredinfoCall;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("同步国内发货信息接口");
    }

    @Before
    public void caseUp() {
        syncdomesticdeliveredinfoBean = new SyncDomesticDeliveredInfoBean();
        syncdomesticdeliveredinfoCall = new SyncDomesticDeliveredInfoCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(syncdomesticdeliveredinfoCall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_SyncDomesticDeliveredInfo_001() {
        Logger.start(true, "同步国内发货信息成功");
        try {
            // 创建订单
            Logger.comment("前置数据准备");
            // 创建一个拼邮的订单
            Map map = new OrderCallService().createM2cOrder(LogisticsTypeEnum.PackageDelivery);
            int orderId = (Integer) map.get("orderId");
            int sellerId = (Integer) map.get("sellerId");
            int userId = (Integer) map.get("userId");
            int tradingId = (Integer) map.get("tradingId");
            String catalogId = (String) map.get("catalogId");
            DecimalFormat df = new DecimalFormat("0.00");// 保留两位小数
            BigDecimal totalPrice = (BigDecimal) map.get("totalPrice");// 支付金额
            // 支付订单
            new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);
            // 接单
            new OrderCallService().acceptOrder(orderId, sellerId);
            // 商家发货
            new OrderCallService().dispatched(orderId, sellerId);
            /*
             * // 确认收货
             * 
             * new OrderCallService().confirmReceive(userId, orderId);
             */
            Logger.comment("前置数据准备结束");

            syncdomesticdeliveredinfoBean.setLogisticsCompany("码头自动化测试物流");
            syncdomesticdeliveredinfoBean.setOrderId(orderId);
            syncdomesticdeliveredinfoBean.setLogisticsNo("autotest_123456");
            syncdomesticdeliveredinfoBean.setWeight(new BigDecimal(2));
            Logger.comment("同步国内发货信息前");
            // 验证订单表
            List<Map> list = new DefaultWapper().selectOrderByOderId(orderId);
            Logger.verifyEquals(null, list.get(0).get("bDomesticDelivered"), "验证订单表bDomesticDelivered");
            syncdomesticdeliveredinfoCall.setData(syncdomesticdeliveredinfoBean);
            syncdomesticdeliveredinfoCall.callService();
            Logger.comment("同步国内发货信息后");
            Logger.verifyEquals("ok", syncdomesticdeliveredinfoCall.getReturnData(), "验证接口返回状态");
            List<Map> list2 = new DefaultWapper().selectOrderByOderId(orderId);
            Logger.debug("bDomesticDelivered:" + list2.get(0).get("bDomesticDelivered"));
            Logger.verifyEquals(true, (Boolean) list2.get(0).get("bDomesticDelivered"), "验证订单表bDomesticDelivered");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_SyncDomesticDeliveredInfo_002() {
        Logger.start(true, "订单号不存在");
        try {
            syncdomesticdeliveredinfoBean.setLogisticsCompany("码头自动化测试物流");
            syncdomesticdeliveredinfoBean.setOrderId(1111);
            syncdomesticdeliveredinfoBean.setLogisticsNo("22222222222");
            syncdomesticdeliveredinfoBean.setWeight(new BigDecimal(2));
            syncdomesticdeliveredinfoCall.setData(syncdomesticdeliveredinfoBean);
            syncdomesticdeliveredinfoCall.callService();
            Logger.verifyEquals("ok", syncdomesticdeliveredinfoCall.getReturnData(), "验证接口返回状态");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_SyncDomesticDeliveredInfo_003() {
        Logger.start(true, "订单号为空");
        try {
            syncdomesticdeliveredinfoBean.setLogisticsCompany("码头自动化测试物流");
            // syncdomesticdeliveredinfoBean.setOrderId(1111);
            syncdomesticdeliveredinfoBean.setLogisticsNo("22222222222");
            syncdomesticdeliveredinfoBean.setWeight(new BigDecimal(2));
            syncdomesticdeliveredinfoCall.setData(syncdomesticdeliveredinfoBean);
            syncdomesticdeliveredinfoCall.callService();
            Logger.verifyEquals("ok", syncdomesticdeliveredinfoCall.getReturnData(), "验证接口返回状态");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
}
